// pages/myPage/myPage.js
var app = getApp();
Page({
    data: {
        code: "获取验证码",
        loginAuthor: false,
        perfect: false,
        last_time: '',
        is_show: true,//获取验证码按钮
        member: "",
        order: "",
        id: "",
        link_jump_flage:""//从哪跳转到登陆页面标识，返回使用
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that=this;
        this.init();
        var flag=options.flag;
        that.setData({
            link_jump_flage: flag
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var that = this;
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        that.init();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },//获取验证码
    clickVerify: function () {
        var that = this;
        var phone_number = that.data.phone;
        if (phone_number) {
            if (!app.phone_number_reg(phone_number)) {
                wx.showToast({
                    title: '请输入正确的手机号',
                    icon: "loading"
                })
            } else {
                // 将获取验证码按钮隐藏60s，60s后再次显示
                that.setData({
                    is_show: !that.data.is_show   //false
                })
                settime(that);
                // 发送验证码
                var params = {};
                params['phone_number'] = phone_number;
                wx.request({
                    url: app.config() + "member/xiaoSendCode",
                    data: params,
                    header: {
                        'content-type': 'application/json'
                    },
                    success: function (res) {
                        // 操作太过于频繁tips
                        if (res.data.status == 4) {
                            wx.showToast({
                                title: '请稍后重试',
                                imagg: "../../images/warning.png"
                            })
                        }
                    }
                })
            }
        } else {
            wx.showToast({
                title: '请输入手机号',
                icon: "loading"
            })
        }
    },//手机号码
    member_phone: function (e) {
        var that = this;
        var phone = e.detail.value;
        that.setData({
            phone: phone
        })
    },//验证码
    verification_code: function (e) {
        var that = this;
        var code = e.detail.value;
        that.setData({
            code: code
        })
    },//提交登陆
    submit: function () {
        var that = this;
        var phone_number = that.data.phone;
        var code = that.data.code;
        var open_id = that.data.memberInfo.open_id;
        var link_jump_flage = that.data.link_jump_flage;
        var goods_id=that.data.goods_id;
        if (!phone_number) {
            return;
        }
        if (!app.phone_number_reg(phone_number)) {
            return;
        }
        if (!code) {
            wx.showToast({
                title: '请输入验证码',
                image: "../../images/warning.png"
            })
            return;
        }
        var params = {};
        params['phone_number'] = phone_number;
        params['verify_code'] = code;
        params['open_id'] = open_id;
        wx.request({
            url: app.config() + "member/xiaoLogANDReg",
            data: params,
            header: {
                'content-type': 'application/json'
            },
            success: function (res) {
                if (res.data.data.status == 0) {
                    var data = res.data.data;
                    wx.showToast({
                        title: '验证成功',
                        icon:'success'
                    })
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1
                        })
                    },1000)
                    wx.setStorage({
                        key: 'memberInfo',
                        data: data,
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        image: "../../images/warning.png"
                    })
                }
            }
        })
    },//初始化页面
    init: function () {
        var that = this;
        wx.getStorage({
            key: 'userInfo',
            success: function (res) {
                if (res.errMsg == "getStorage:ok") {
                    var nickName = res.data.nickName;
                    var avatarUrl = res.data.avatarUrl;
                    var open_id = res.data.open_id;
                    that.setData({
                        loginAuthor: false,
                        nickName: nickName,
                        avatarUrl: avatarUrl,
                        perfect: true,
                        memberInfo:res.data
                    })
                }
            },
            fail: function () {
            }
        })
        // 检验登陆状态
        wx.getStorage({
            key: 'memberInfo',
            success: function (res) {
                var data = res.data;
                var member_id = data.id;
                if (member_id) {
                    that.setData({
                        n_login: false,
                        y_login: true,
                        member_id: member_id
                    })
                }
            }
        })
        // 判断是从那个页面跳转到登陆页面的
        wx.getStorage({
            key: 'link_jump_flage',
            success: function(res) {
                var data=res.data;
                var link_jump_flage = data.link_jump_flage;
                var goods_id=data.goods_id;
                that.setData({
                    link_jump_flage: link_jump_flage,
                    goods_id:goods_id
                })
            },
        })
    }
})


var countdown = 60;
var settime = function (that) {
    if (countdown == 0) {
        that.setData({
            is_show: true,
            last_time: ""
        })
        countdown = 60;
        return;
    } else {
        that.setData({
            is_show: false,
            last_time: countdown
        })
        countdown--;
    }
    setTimeout(function () {
        settime(that)
    }, 1000)
}